#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>

/*
 * Params: k: array of possible action cards
 * Description: This function generates an integer representing a
 * 			    randomly choosen action card.
 * Return: Return the random integer representing action card.
 */
int randomActionCard(int *k, int length)
{
	//set up random number generator
	int seed = 1000;
	SelectStream(1);
	PutSeed((long)seed);

	//Initialization
	int position = rand() % (length - 1);
	int result = k[position];

	return result;
}

void randomGreatHallTest()
{
	//Initialization
	int i = 0;
	int player = 0;
	int iterationCount = 0;
	int randomTestCount = 1000;
	int shouldBeHandCount = 0;
	int shouldBeActionCount = 0;
	int notNeeded = 0;
	int handPos = 0;
	struct gameState G;

	while(iterationCount < randomTestCount)
	{
		iterationCount++;
		initializeRandomGameState(&G, &shouldBeActionCount, &shouldBeHandCount);
		//set great_hall hand position
		for(i = 0; i < MAX_HAND; i++)
		{
			if(G.hand[player][i] == great_hall)
			{
				handPos = i;
			}
		}
		
		cardEffect(great_hall, notNeeded, notNeeded, notNeeded, &G, handPos, &notNeeded);
		printf("Iteration %d out of %d ", iterationCount, randomTestCount);
		assert(G.handCount[player] == shouldBeHandCount);
		assert(G.numActions == shouldBeActionCount);
		
		printf("passed\n");
	}
	printf("Random Tester Passed!\n");
}

/*
 * Params: G:		GameState to Initialize
 * 		   shouldBeActionCount: address of variable which will be used
 * 		   						to assert what the action count 
 * 		   						should be
 *		   shouldBeHandCount: address of variable which will be used
 *		   					  to assert what the hand count should be
 * Description: This function intializes a ranomd gameState.
 * Return: None. Game State and "Shoul Be" values can be
 * 				 accessed else where after function completed.
 */
void initializeRandomGameState(struct gameState *G, int* shouldBeActionCount, int* shouldBeHandCount)
{
	//set up random number generator
	int seed = 1000;
	SelectStream(1);
	PutSeed((long)seed);

	//Intialization
	int i = 0;
	int player = 0;
	int numPlayers = 1;
	int currentHandCount = 0;
	int numHandCount = 4;
	int addOneCard = 1;
	int numEstateCards = rand() % 10 + 1;
	int numCopperCards = rand() % 10 + 1;
	int toHand = 2;
	int handPos = 0;
	int currentActionCount = 0; 
	int k[10] = {adventurer, council_room, feast, gardens, mine,
				 remodel, smithy, village, baron, adventurer};
	int length = 10;

	//clear gameState
	memset(G, 23, sizeof(struct gameState));
	
	//set game state states
	G->numPlayers = numPlayers;
	G->whoseTurn = player;
	G->playedCardCount = 0;
	

	//set player deck with copper
	G->deckCount[player] = 0;
	for(i = 0; i < numCopperCards; i++)
	{
		G->deck[player][i] = estate;
		G->deckCount[player]++;
	}
	
	//set player deck with estate 
	for(i = 0; i < numEstateCards; i++)
	{
		G->deck[player][i] = copper;
		G->deckCount[player]++;
	}
	
	//shuffle player decks
	if(shuffle(player, G) < 0)
	{
		printf("Shuffling Error\n");
		exit(EXIT_FAILURE);
	}
	
	//set discard pile to 0
	G->discardCount[player] = 0;
	
	//set player hand
	G->handCount[player] = numHandCount;
	G->hand[player][0] = randomActionCard(k, length);
	G->hand[player][1] = randomActionCard(k, length);
	G->hand[player][2] = randomActionCard(k, length);
	G->hand[player][3] = randomActionCard(k, length);
	G->hand[player][4] = randomActionCard(k, length);
	
	//tracking hand count for first player
	currentHandCount = G->handCount[player];
	*shouldBeHandCount = currentHandCount + addOneCard;
	
	//tracking action number for first player
	currentActionCount = G->numActions;
	*shouldBeActionCount = currentActionCount + addOneCard;
	
	//add great_hall card
	gainCard(great_hall, G, toHand, player);
}

int main ()
{
	srand(time(NULL));
	randomGreatHallTest();
	return 0;
}
